package com.klun.concTrueWar.five;

import java.util.concurrent.CountDownLatch;

/**
 * 在计时测试中使用 CountDownLatch 来启动和停止线程
 * 闭锁：可以延迟线程的进度 直到其达到终止状态
 */
public class TestHarness {
    public long timeTasks(int nThreads ,final Runnable task) throws InterruptedException{
        final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate =  new CountDownLatch(nThreads);
        for (int i = 0; i < nThreads; i++) {
            Thread t = new Thread(){
                public void run(){
                    try{
                        startGate.await();
                        try{
                            task.run();
                        }finally {
                            endGate.countDown();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        }

        long start = System.nanoTime();
        startGate.countDown();
        endGate.await();
        long end = System.nanoTime();
        return end-start;
    }
}
